package net.oschina.app.ui;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import net.oschina.app.R;
import net.oschina.app.bean.NewsList;
import net.oschina.app.util.XmlUtils;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    //【三】ButterKnife使用
    // 3.1 通过ButterKnife注解式声明view
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tabhost)
    FragmentTabHost mTabhost;

    //3.2 注解式声明事件
    @OnClick(R.id.iv_quick_option)
    public void quickOption(View v){
        Toast.makeText(this, "点击中间图标，解析xml数据", Toast.LENGTH_SHORT).show();
        parseXml();
    }



    @OnClick(R.id.toolbar)
    public void testEvent(View v) {
        Toast.makeText(this, "点击Toolbar", Toast.LENGTH_SHORT).show();
    }

    @OnCheckedChanged(R.id.checkbox)
    //方法的参数要与该监听事件接口的方法参数一致 ,即OnCheckedChanged的回调方法需传CompoundButton，boolean两个参数
    public void testCheckBox(CompoundButton buttonView, boolean isChecked) {
        Toast.makeText(this, isChecked == true ? "按钮选中" : "按钮取消", Toast.LENGTH_SHORT).show();
    }




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //3.3 通过ButterKnife注入控件及事件绑定
        ButterKnife.bind(this);
        //1. 初始Toolbar
        initToolbar();


        //4. 初始化FragmentTabhost
       initTabhost();
    }



    //==========================【四】 FragmentTabhost 实现底部标签 begin=======
    //4 . 初始化TabHost
    private void initTabhost() {
        //4.1 初始化底部标签
        /**
         * 设置TabHost
         * context :上下文
         * manager ：片段管理器
         * containerId:装载片段的容器
         */
        mTabhost.setup(this, getSupportFragmentManager(), R.id.fl_realcontent);
        if(android.os.Build.VERSION.SDK_INT>10){ //大于10版本，不显示分割线
            //标签小部件，是一个View，
            TabWidget tabWidget = mTabhost.getTabWidget();
            //不显示分割线
            tabWidget.setShowDividers(0);
        }

        // 4.2 往FragmentTabHost ：添加tab标签
        addTabs();

    }

    //添加底部导航标签
    private void addTabs() {
        MainTab[] tabs = MainTab.values();
        final int size = tabs.length;
        for (int i = 0; i < size; i++) {
            // 找到每一个枚举的Fragment对象
            MainTab mainTab = tabs[i];
            // 1. 创建标签
            TabHost.TabSpec tab = mTabhost.newTabSpec(this.getResources().getString(mainTab.getResName()));
            // 初始化标签 ↓
            View indicator = LayoutInflater.from(getApplicationContext())
                    .inflate(R.layout.tab_indicator, null);
            TextView title = (TextView) indicator.findViewById(R.id.tab_title);
            Drawable drawable = this.getResources().getDrawable(
                    mainTab.getResIcon());
            //设置TextView上下左右的绘图
            title.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
                    null);
            if (i == 2) {
                //当是正中间的标签，则隐藏 ，用于显示ImageView 弹出对话框的控件
                indicator.setVisibility(View.INVISIBLE);
            }
            title.setText(getString(mainTab.getResName()));
            //设置标签的指示器，是一个View
            tab.setIndicator(indicator);

            //2. 设置要传递给Fragment的参数
            Bundle bundle = new Bundle();
            bundle.putString("key",
                    "content: " + getString(mainTab.getResName()));
            // 3. 把新的标签添加到TabHost中
            mTabhost.addTab(tab, mainTab.getClz(), bundle);
        }
    }

    //==========================【四】 FragmentTabhost 实现底部标签 end=========






    //==========================【五】用xstream解析xml begin=======
    private void parseXml() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //资讯模块的url地址
                    URL url = new URL("http://10.0.2.2:8080/oschina/list/news/page0.xml");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(5000);
                    conn.connect();
                    int code = conn.getResponseCode();
                    if (code == 200) {
                        //从服务端获取xml数据流
                        InputStream is = conn.getInputStream();
                        NewsList newsList = XmlUtils.toBean(NewsList.class, is);
                        Log.e("xstream",newsList.getList().get(0).getBody());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //==========================【五】用xstream解析xml end=========




    //==========================【一】使用Toolbar   begin=======
    private void initToolbar() {
        //1. 引用Toolbar
//        Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
        //2. 设置Toolbar取代Actionbar
        setSupportActionBar(toolbar);

        //3. 设置Toolbar标签
        toolbar.setNavigationIcon(R.drawable.ic_drawer);

        //4. 对导航图标设置监听
/*        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "toolbar导航图标监听", Toast.LENGTH_SHORT).show();
            }
        });*/

    }

    //==========================【一】使用Toolbar end=========





    //==========================【二】添加MenuItem begin=======
    //2.1 创建菜单项
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //装配菜单布局
        getMenuInflater().inflate(R.menu.search_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    //2.2 监听菜单按钮

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search_content: //搜索按钮
                Toast.makeText(this, "搜索", Toast.LENGTH_SHORT).show();
                break;
            case android.R.id.home: //点击回退按钮：toolbar或者Actionbar的回退按钮
                Toast.makeText(this, "回退按钮", Toast.LENGTH_SHORT).show();
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick({R.id.tv_content, R.id.checkbox})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_content:
                break;
            case R.id.checkbox:
                break;
        }
    }
    //==========================【二】添加MenuItem  end=========

}
